package com.cqxyd.smartsite.service.impl;

import com.cqxyd.smartsite.dao.DeviceInfoMapper;
import com.cqxyd.smartsite.dao.ProgramInfoMapper;
import com.cqxyd.smartsite.pojo.DeviceInfo;
import com.cqxyd.smartsite.pojo.ProgramInfo;
import com.cqxyd.smartsite.pojo.ProgramInfo;
import com.cqxyd.smartsite.service.ProgramInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Angus Yu
 * @date 2020-05-19 16:00
 */
@Service
public class ProgramInfoServiceImpl implements ProgramInfoService {

    @Autowired
    private ProgramInfoMapper programInfoMapper;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public int insert(ProgramInfo programInfo) {
        return programInfoMapper.insert(programInfo);
    }

    @Override
    public int update(ProgramInfo programInfo) {
        return programInfoMapper.updateByPrimaryKey(programInfo);
    }

    @Override
    @Transactional
    public int delete(Integer programId) throws Exception {
        try {
            Example example = new Example(DeviceInfo.class);
            Example.Criteria criteria = example.createCriteria();
            if (programId != null) {
                criteria.andCondition("program_id =" + programId);
                deviceInfoMapper.deleteByExample(example);
            }
        } catch (Exception e) {
            throw e;
        }

        try {
            programInfoMapper.deleteByPrimaryKey(programId);
        } catch (Exception e) {
            throw e;
        }

        return 1;
    }

    @Override
    public ProgramInfo selectOne(Integer programId) {
        return programInfoMapper.selectByPrimaryKey(programId);
    }

    @Override
    public PageInfo<ProgramInfo> getProgramInfoListByPage(Integer pageNum, Integer pageSize, String programName) {
        Example example = new Example(ProgramInfo.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(programName)) {
            criteria.andCondition("program_name like'%" + programName + "%'");
        }
        example.setOrderByClause("`program_id` ASC");
        List<ProgramInfo> sysConfigList = programInfoMapper.selectByExample(example);
        PageInfo<ProgramInfo> pageInfo = new PageInfo<ProgramInfo>(sysConfigList);
        return pageInfo;
    }

    @Override
    public List<String> getKeyAndSecret(String serialNo) {
        List<String> keyAndSecret = new ArrayList<String>();
        DeviceInfo deviceInfoSearch = new DeviceInfo();
        deviceInfoSearch.setSerialNo(serialNo);
        DeviceInfo deviceInfo = deviceInfoMapper.selectOne(deviceInfoSearch);
        if (deviceInfo == null) {
            return null;
        }
        ProgramInfo programInfo = programInfoMapper.selectByPrimaryKey(deviceInfo.getProgramId());
        if (programInfo == null) {
            return null;
        }
        keyAndSecret.add(programInfo.getProgramKey());
        keyAndSecret.add(programInfo.getProgramSecret());
        return keyAndSecret;
    }
}
